package com.thb.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.entity.ChannelConfig;
import com.thb.pay.mapper.ChannelConfigMapper;
import com.thb.pay.service.ChannelConfigService;
import com.thb.pay.vo.req.ChannelConfigPageReq;
import com.thb.pay.vo.req.ChannelConfigReq;
import com.thb.pay.vo.resp.ChannelConfigVo;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @Description 通道配置信息表 服务实现类
 * @Author lucky
 * @data 2019/9/26 22:34
 */
@Slf4j
@Service
public class ChannelConfigServiceImpl extends BaseServiceImpl<ChannelConfigMapper, ChannelConfig> implements ChannelConfigService {
    @Override
    public PageResult<ChannelConfigVo> findPage(ChannelConfigPageReq pageReq) {
        IPage<ChannelConfig> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<ChannelConfig> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, ChannelConfigVo.class);
    }

    @Override
    public void add(ChannelConfigReq channelConfigReq) {
        Assert.notNull(channelConfigReq, "channelConfigReq.not.null");
        Assert.notNull(channelConfigReq.getChannelId(), "channelId.not.null");
        ChannelConfig channelConfig = new ChannelConfig();
        BeanUtil.copyProperties(channelConfigReq, channelConfig);
        super.insert(channelConfig);
    }

    @Override
    public void update(ChannelConfigReq channelConfigReq) {
        Assert.notNull(channelConfigReq, "channelConfigReq.not.null");
        Assert.notNull(channelConfigReq.getId(), "channelConfigId.not.null");
        ChannelConfig channelConfig = super.selectById(channelConfigReq.getId());
        Assert.notNull(channelConfig, "channelConfig.not.null");
        ChannelConfig targetChannelConfig = new ChannelConfig();
        BeanUtil.copyProperties(channelConfigReq, targetChannelConfig);
        super.updateById(targetChannelConfig);
    }

    @Override
    public ChannelConfigVo getById(Long id) {
        Assert.notNull(id, "channelConfigId.not.null");
        ChannelConfig channelConfig = super.selectById(id);
        if (channelConfig != null) {
            ChannelConfigVo channelConfigVo = new ChannelConfigVo();
            BeanUtil.copyProperties(channelConfig, channelConfigVo);
            return channelConfigVo;
        }
        return null;
    }

    @Override
    public ChannelConfigVo getByChannelId(String channelId) {
        Assert.notNull(channelId, "channelId.not.null");
        LambdaQueryWrapper<ChannelConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChannelConfig::getChannelId, channelId);
        List<ChannelConfig> channelConfigList = super.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(channelConfigList)) {
            ChannelConfigVo channelConfigVo = new ChannelConfigVo();
            BeanUtil.copyProperties(channelConfigList.get(0), channelConfigVo);
            return channelConfigVo;
        }
        return null;
    }
}
